#include "GroceryListViewList.h"
#include "GroceryListView.h"

GroceryListViewList::GroceryListViewList(GroceryListView* glv, map<String, vector<Eatable> >& items) :
	glView(glv), groceryItems(items), offset(0) {

}

void GroceryListViewList::paint(Graphics& g) {
	Font bold(Font(Font::getDefaultSansSerifFontName(), 18, Font::bold));
	Font plain(Font(Font::getDefaultSansSerifFontName(), 18, Font::plain));
	Rectangle r(0, 0, getWidth(), getHeight());
	g.fillAll(Colours::white);
	g.setColour(Colours::beige);
	g.fillRect(r);
	g.setColour(Colours::grey);
	g.drawRect(r);

	g.setFont(plain);
	g.setColour(Colours::darkgrey);
	int currentY = 50;

	if (groceryItems.empty()) {
		g.drawMultiLineText(
				T("Select food items and click the Add button to add them to your grocery list"),
				35, 50, getWidth() - 50);
		return;
	}

	for (map<String, vector<Eatable> >::const_iterator mit = groceryItems.begin(); mit
			!= groceryItems.end(); ++mit) {
		if (mit->second.size()) {
			g.setFont(bold);
			g.drawSingleLineText(mit->first, 35, currentY);
			currentY += 20;
			g.setFont(plain);
			for (vector<Eatable>::const_iterator vit = mit->second.begin(); vit != mit->second.end(); ++vit) {
				if (mouseY >= currentY - 15 && mouseY <= currentY) {
					g.setColour(Colours::lightblue);
					g.fillRect(54, currentY - 14, r.getRight() - 55, 18);
					if (mouseX >= 33 && mouseX <= 51) {
						g.setColour(Colour(250, 170, 140));
						setMouseCursor(MouseCursor::PointingHandCursor);
					} else {
						g.setColour(Colour(190, 200, 230));
						setMouseCursor(MouseCursor::NormalCursor);
					}
					g.fillRoundedRectangle(33, currentY - 13, 18, 16, 2);
					g.setColour(Colours::grey);
					g.drawRoundedRectangle(33, currentY - 13, 18, 16, 2, 1);
					g.setColour(Colours::black);
					g.drawSingleLineText(T("X"), 37, currentY + 1);
					g.setColour(Colours::darkgrey);
				}
				g.drawSingleLineText(vit->getName(), 55, currentY);
				currentY += 18;
			}
			currentY += 5;
		}
	}
}

void GroceryListViewList::mouseDown(const MouseEvent& e) {
	Rectangle r(0, 0, getWidth() / 2, getHeight());

	if (!(e.x >= 33 && e.x <= 51)) {
		return;
	}

	String toRemoveHeader;
	vector<Eatable>::iterator toRemove;
	bool doRemove = false;

	int currentY = 50 + offset;
	for (map<String, vector<Eatable> >::iterator mit = groceryItems.begin(); mit != groceryItems.end(); ++mit) {
		if (mit->second.size()) {
			currentY += 20;
			for (vector<Eatable>::iterator vit = mit->second.begin(); vit != mit->second.end(); ++vit) {
				if (e.y >= currentY - 15 && e.y <= currentY) {
					if (e.x >= 33 && e.x <= 51) {
						doRemove = true;
						toRemove = vit;
						toRemoveHeader = mit->first;
						break;
					}
				}
				currentY += 18;
			}
			currentY += 5;
		}
	}

	if (doRemove) {
		groceryItems[toRemoveHeader].erase(toRemove);
		setBounds(0, 0, getWidth(), std::max(glView->getHeight() - 55, getTotalHeight()));
		repaint();
		glView->repaint();
	}
}

void GroceryListViewList::mouseMove(const MouseEvent& e) {
	mouseX = e.x;
	mouseY = e.y;
	repaint();
}

void GroceryListViewList::setCurrentYOffset(int offset) {
	this->offset = offset;
}

int GroceryListViewList::getTotalHeight() {
	int totalHeight = 50;
	for (map<String, vector<Eatable> >::iterator mit = groceryItems.begin(); mit != groceryItems.end(); ++mit) {
		if (mit->second.size()) {
			totalHeight += 18 * mit->second.size() + 25;
		}
	}
	return totalHeight + 30;
}
